package com.winton.common;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Tools {
	
	/**
	 * Date转str
	 * @param date
	 * @return dateStr
	 */
	public static String getDate(Date date) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 * Datetime转str
	 * @param date
	 * @return dateTime
	 */
	public static String getDateTime(Date date) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String dateTime = sdf.format(date);
		return dateTime;
	}
	
	/**
	 * 加密算法
	 * @param str
	 * @return
	 */
	public static String getMD5(String str) {
	
	        // 生成一个MD5加密计算摘要
	        MessageDigest md = null;
			try {
				md = MessageDigest.getInstance("MD5");
				// 计算md5函数
		        md.update(str.getBytes());
		        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
		        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
		        
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
			return new BigInteger(1, md.digest()).toString(16);
	}
	
	/**
	 * str转Date
	 * @param strDate
	 * @return
	 */
	public static Date getStringToDate(String strDate,String formate) {
		SimpleDateFormat sdf=new SimpleDateFormat(formate);  
		Date date = new Date();
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		return date;
	}
	
	
	/** 
     * 获取客户端IP 
     * @param HttpServletRequest 
     * @return 
     */  
    public static String getRequestIP(javax.servlet.http.HttpServletRequest request){
    	try {
        String ipAddress = request.getHeader("x-forwarded-for");  
            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
                ipAddress = request.getHeader("Proxy-Client-IP");  
            }  
            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
                ipAddress = request.getHeader("WL-Proxy-Client-IP");  
            }  
            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {  
                ipAddress = request.getRemoteAddr(); 
            }  
            //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
            if(ipAddress!=null && ipAddress.length() > 15){
            	//"***.***.***.***".length() = 15  
                if(ipAddress.indexOf(",")>0){  
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }  
            }  
            return ipAddress;
    	} catch(Exception ex) {
    		return "";
    	}
    }
	
    /**
     * 获取每页开始记录
     * @param pageIndex 当前页码
     * @param pageSize  每页记录数
     * @return
     */
    public static int getPageStartIndex(int pageIndex,int pageSize) {
    	return (pageIndex - 1) * pageSize;
    }
    /**
     * 获取每页结束记录
     * @param pageIndex 当前页码
     * @param pageSize  每页记录数
     * @return
     */
    public static int getPageEndIndex(int pageIndex,int pageSize) {
    	return pageIndex * pageSize;
    }
	

	public static void main(String[] args) {
		String value = "测试  一下";
		for(int i = 0;i < value.split(" ").length;++i){
			if(!"".equals(value.split(" ")[i])){
				System.out.println(value.split(" ")[i]);
			}
		}
		
//		UUID uuid = UUID.randomUUID();
//		System.out.println(".{"+uuid.toString()+"}");

//		System.out.println("-----日期------"+getDate(new Date()));
//		System.out.println("-----时间------"+getDateTime(new Date()));
//		System.out.println("-----MD5------"+getMD5("mwyuupllisa"));
//		System.out.println("-----str转date------"+getStringToDate("05-25-2017 13:56:18","MM-dd-yyyy HH:mm:ss"));
		

	}

}
